package top.javarem.medical.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import top.javarem.medical.domain.dto.MedicalKnowledgeDTO;
import top.javarem.medical.domain.po.Knowledge;
import top.javarem.medical.mapper.KnowledgeMapper;
import top.javarem.medical.service.KnowledgeService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @description 针对表【knowledge(中医科普知识库)】的数据库操作Service实现
 */
@Service
public class KnowledgeServiceImpl extends ServiceImpl<KnowledgeMapper, Knowledge>
        implements KnowledgeService {

    @Resource
    private RedissonClient redissonClient;

    @Override
    public List<MedicalKnowledgeDTO> queryByCategoryId(Integer categoryId) {
//        先从缓存获取
        String cacheKey = "Medical_Knowledge_Key_" + categoryId;
        RBucket<List<MedicalKnowledgeDTO>> bucket = redissonClient.<List<MedicalKnowledgeDTO>>getBucket(cacheKey);
        List<MedicalKnowledgeDTO> medicalKnowledgeDTOList = bucket.get();
        if (!CollectionUtils.isEmpty(medicalKnowledgeDTOList))
            return medicalKnowledgeDTOList;
//        再从数据库查询
        List<Knowledge> knowledgeList = lambdaQuery().eq(Knowledge::getCategoryId, categoryId)
                .eq(Knowledge::getStatus, 1)
                .list();
        medicalKnowledgeDTOList = new ArrayList<>();
        for (Knowledge knowledge : knowledgeList) {
            MedicalKnowledgeDTO medicalKnowledgeDTO = new MedicalKnowledgeDTO();
            BeanUtils.copyProperties(knowledge, medicalKnowledgeDTO);
            medicalKnowledgeDTOList.add(medicalKnowledgeDTO);
        }//三小时有效期
        bucket.set(medicalKnowledgeDTOList, 3, TimeUnit.HOURS);
        return medicalKnowledgeDTOList;

    }

    @Override   //分页查询
    public List<MedicalKnowledgeDTO> pageQuery(Integer categoryId, String title,
                                               Integer pageNum, Integer pageSize) {
        List<Knowledge> knowledgeList = lambdaQuery().eq(categoryId != null, Knowledge::getCategoryId, categoryId)
                .like(StringUtils.hasText(title), Knowledge::getTitle, title)
                .eq(Knowledge::getStatus, 1)
                .orderByDesc(Knowledge::getCreatedAt)
                .last("limit " + (pageNum - 1) * pageSize + "," + pageSize)
                .list();
        List<MedicalKnowledgeDTO> medicalKnowledgeDTOList = new ArrayList<>();
        for (Knowledge knowledge : knowledgeList) {
            MedicalKnowledgeDTO medicalKnowledgeDTO = new MedicalKnowledgeDTO();
            BeanUtils.copyProperties(knowledge, medicalKnowledgeDTO);
            medicalKnowledgeDTOList.add(medicalKnowledgeDTO);
        }
        return medicalKnowledgeDTOList;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    //添加
    public boolean addKnowledge(MedicalKnowledgeDTO dto) {

        Knowledge knowledge = new Knowledge();
        knowledge.setTitle(dto.getTitle());
        knowledge.setCategoryId(dto.getCategoryId());
        knowledge.setStatus(1);
        knowledge.setCreatedAt(new Date());
        knowledge.setUpdatedAt(new Date());
        knowledge.setContent(dto.getContent());
        knowledge.setSubtitle(dto.getSubtitle());
        return save(knowledge);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    //修改
    public boolean updateKnowledge(MedicalKnowledgeDTO dto) {

        Knowledge knowledge = new Knowledge();
        BeanUtils.copyProperties(dto, knowledge);
        return updateById(knowledge);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteKnowledge(Long id) {
        return removeById(id);
    }

}
